package com.woniuxy.cq.phrase3.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.cq.phrase3.common.Result;
import com.woniuxy.cq.phrase3.dao.MessageMapper;
import com.woniuxy.cq.phrase3.dao.ProjectMapper;
import com.woniuxy.cq.phrase3.dao.SxMapper;
import com.woniuxy.cq.phrase3.dao.UserMapper;
import com.woniuxy.cq.phrase3.entity.Project;
import com.woniuxy.cq.phrase3.entity.ProjectIndustryUser;
import com.woniuxy.cq.phrase3.entity.Sbp;
import com.woniuxy.cq.phrase3.entity.Sx;
import com.woniuxy.cq.phrase3.service.ISxService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName Examine
 * @Description TODO
 * @createTime 2020年09月07日 09:43:00
 */
@RestController
@Slf4j
public class ExamineController {


    @Autowired
    private SxMapper sxMapper;

    @Autowired
    private ISxService sxService;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private MessageMapper messageMapper;

    /**
     * @description: 分页查询所有本人本部门事项
     * @param:
     * @return: com.woniuxy.cq.phrase3.common.Result
     * @author Daniel
     * @date: 2020/9/7 10:51
     */
    @GetMapping("/examine/queryAll")
    @RequiresRoles("reviewer")
    public Result select(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize, HttpSession session, String sxStatus, String sxUrgency) {

        log.info("传入数据{},{}", sxStatus, sxUrgency);
        Integer userId = (Integer)session.getAttribute("userId");

        System.out.println("-----------------------------------------------------"+userId);
        IPage<Sbp> sxIpage = sxService.pageQuery2(pageIndex, pageSize, userId, sxStatus, sxUrgency);
        return Result.success(sxIpage);
    }
    /** 
     * @description: 回收未通过事项,伪删除
     * @param: id 
     * @return: com.woniuxy.cq.phrase3.common.Result 
     * @author Daniel
     * @date: 2020/9/10 14:43
     */
    @PostMapping("/examine/delete")
    @RequiresRoles("reviewer")
    public Result delete(Integer id) {
        log.info("传入参数{}", id);
        Sx sx = new Sx();
        sx.setSxId(id);
        sx.setSxStatus("FAIL");
        sx.setSxContent("未通过");
        int rows = sxMapper.updateById(sx);
        if (rows != 0) {
            return Result.success();
        }
        return Result.fail("0000", "修改失败");
    }
    /**
     * @description:  修改审查状态
     * @param: sxId
     * @param: sxStatus
     * @return: com.woniuxy.cq.phrase3.common.Result
     * @author Daniel
     * @date: 2020/9/10 14:24
     */
    @PostMapping("/examine/update")
    @RequiresRoles("reviewer")
    public Result update(Integer sxId, String sxStatus) {
        log.info("传入参数{},{}", sxId, sxStatus);
        if (sxStatus.equals("PASS")) {
            Sx sx = sxMapper.selectById(sxId);
            Integer sxProjectId = sx.getSxProjectId();
            int i = selectSxTotal(sxProjectId);
            Sx sx1 = new Sx();
            sx1.setSxId(sxId);
            sx1.setSxStatus(sxStatus);
            sx1.setSxContent("审查通过");
            int rows = sxMapper.updateById(sx1);
            if (rows != 0) {
                return Result.success();
            } else {
                return Result.fail("0000", "修改失败");
            }

        } else {
            Sx sx = new Sx();
            sx.setSxId(sxId);
            sx.setSxStatus(sxStatus);
            sx.setSxContent("资料不齐全，请补齐资料后再次审查");
            int i = sxMapper.updateById(sx);
            //加入消息队列
            Sx sx1 = sxMapper.selectById(sxId);
            Integer sxProjectId = sx1.getSxProjectId();
            Project project = projectMapper.selectById(sxProjectId);
            Integer projectUserId = project.getProjectUserId();
            String projectName = project.getProjectName();
            String message="您的项目"+projectName+"资料不齐全，请补齐资料后再次审查";
            messageMapper.insertNewMessage(projectUserId,message);
            if (i != 0) {
                return Result.success();
            } else {
                return Result.fail("0000", "更新失败");
            }
        }
    }
    /**
     * @description: 封装审查剩余事项，判断审查是否全部通过
     * @param: projectId
     * @return: int
     * @author Daniel
     * @date: 2020/9/10 14:24
     */
    public int selectSxTotal(Integer projectId) {
        Project project = projectMapper.selectById(projectId);
        Integer projectSxTotal = project.getProjectSxTotal();
        if (projectSxTotal == 1) {
            Project project1 = new Project();
            //通过审查
            project1.setProjectStatus("SUCCESS_CHECK");
            project1.setProjectId(projectId);
            //4:决策阶段
            project1.setProjectPhase(3);
            project1.setProjectSxTotal(0);
            project1.setProjectUpdateTime(new Date());
            project1.setProjectResult("审查通过，待决策");
            projectMapper.updateById(project1);

            //加入消息队列
            Integer projectUserId = project.getProjectUserId();
            String projectName = project.getProjectName();
            String message = "恭喜您，你的项目"+projectName+"审查通过，进入待决策阶段";
            messageMapper.insertNewMessage(projectUserId,message);
            return 1;
        } else {
            int newProjectSxTotal = projectSxTotal - 1;
            Project project2 = new Project();
            project2.setProjectId(projectId);
            project2.setProjectSxTotal(newProjectSxTotal);
            projectMapper.updateById(project2);
            return 2;
        }
    }

    /**
     * @description: 查询详细信息
     * @param: sxId
     * @return: com.woniuxy.cq.phrase3.common.Result
     * @author Daniel
     * @date: 2020/9/10 14:23
     */
    @GetMapping("/examine/projectInfo")
    @RequiresRoles("reviewer")
    public Result getProjectInfo(Integer sxId){
        log.info("传入的事项id:{}",sxId);
        Sx sx = sxMapper.selectById(sxId);
        Integer sxProjectId = sx.getSxProjectId();
        ProjectIndustryUser projectInfo = projectMapper.getProjectInfo(sxProjectId);
        return Result.success(projectInfo);
    }
}
